package com.peng.crm.workbench.controller;

import com.peng.crm.commons.utils.DateUtil;
import com.peng.crm.commons.utils.Result;
import com.peng.crm.commons.utils.UUIDUtil;
import com.peng.crm.settings.entity.User;
import com.peng.crm.workbench.entity.ActivityRemark;
import com.peng.crm.workbench.service.ActivityRemarkService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * 市场活动表(ActivityRemark)表控制层
 *
 * @author makejava
 * @since 2022-07-02 19:11:30
 */
@RestController
@RequestMapping("/activityRemark")
public class ActivityRemarkController {
    /**
     * 服务对象
     */
    @Resource
    private ActivityRemarkService activityRemarkService;

    /**
     * 列表查询查询
     *
     * @param activityID   筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public Result list(String activityID) {
        return Result.success(activityRemarkService.list(activityID));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/find/{id}")
    public Result  queryById(@PathVariable("id") String id) {
        return Result.success(activityRemarkService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param activityRemark 实体
     * @return 新增结果
     */
    @PostMapping("/save")
    @ResponseBody
    public Result  save(@RequestBody ActivityRemark activityRemark, HttpSession session) {
        User loginUser = (User)session.getAttribute("loginUser");
        //手动维护ID
        activityRemark.setId(UUIDUtil.getUUID());
        //设置创建者，修改者
        activityRemark.setCreateBy(loginUser.getLoginAct());
        activityRemark.setEditBy(loginUser.getLoginAct());
        //设置创建时间，修改时间
        activityRemark.setCreateTime(DateUtil.formatDate(new Date()));
        activityRemark.setEditTime(DateUtil.formatDate(new Date()));
        return Result.success(activityRemarkService.insert(activityRemark));
    }

    /**
     * 编辑数据
     *
     * @param activityRemark 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @ResponseBody
    public Result  edit(@RequestBody ActivityRemark activityRemark,HttpSession session) {
        //从session域中取到当前登陆的用户
        User loginUser = (User)session.getAttribute("loginUser");
        //设置修改者
        activityRemark.setEditBy(loginUser.getLoginAct());
        //设置修改时间
        activityRemark.setEditTime(DateUtil.formatDate(new Date()));
        return Result.success(activityRemarkService.update(activityRemark));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteById/{id}")
    @ResponseBody
    public Result deleteById(@PathVariable("id") String id) {
        return Result.success(activityRemarkService.deleteById(id));
    }


}

